package tool

import (
	consts2 "MoSkeleton/config/consts"
	"MoSkeleton/framework/core/consts"
	"MoSkeleton/framework/core/dto/request"
	"MoSkeleton/framework/core/dto/response"
	"MoSkeleton/framework/core/moerrors"
	"MoSkeleton/framework/utils"
	"MoSkeleton/global"
	"MoSkeleton/services"
	"MoSkeleton/web/pogos/request/tool"
	"MoSkeleton/web/v1/http/base"
	"errors"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type GenTemplateClassifyCtrl struct {
	base.BaseCtrl
}

func (g *GenTemplateClassifyCtrl) GetCtrlTitle() string {
	msg, _ := utils.Translater.Translate(consts.TKey_TitleGenTemplateClassifyMsg)
	return msg
}

// GetClassifyPage 获取分类列表
// @Tags 代码模板分类
// @Summary 获取分类列表
// @Security ApiKeyAuth
// @Produce  application/json
// @Param data body tool.GenTemplateClassifyPageReq true "获取分类列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "{"code": 200, "data": [...],"msg": "ok"}"
// @Router /tool/codegen/classify/list [get]
func (g *GenTemplateClassifyCtrl) GetClassifyPage(c *gin.Context) {
	pageReq := new(tool.GenTemplateClassifyPageReq)
	if err := c.ShouldBindQuery(pageReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	pageReq.ShouldBindParams(c)
	classifyPageResp, err := services.ServiceGroupInst.ToolGroupInst.GenTemplateClassifyServiceInst.GetClassifyPage(pageReq)
	if err != nil {
		global.MoLogger.Error("获取分类列表失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_TemplateClassifyGetListFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, classifyPageResp)
}

// GetAllClassifys 获取所有分类
// @Tags 代码模板分类
// @Summary 获取所有分类
// @Security ApiKeyAuth
// @Produce  application/json
// @Success 200 {object} response.Response{data=[]tool.GenTemplateClassifySimpleItemResp,msg=string} "{"code": 200, "data": [...],"msg": "ok"}"
// @Router /tool/codegen/classify/getAll [get]
func (g *GenTemplateClassifyCtrl) GetAllClassifys(c *gin.Context) {
	classifySimplItemsResp, err := services.ServiceGroupInst.ToolGroupInst.GenTemplateClassifyServiceInst.GetAllClassifys()
	if err != nil {
		global.MoLogger.Error("获取所有分类失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ServerOccurredErrorCode, consts.TKey_TemplateClassifyGetAllFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, classifySimplItemsResp)
}

// AddClassify 添加分类
// @Tags 代码模板分类
// @Summary 添加分类
// @Security ApiKeyAuth
// @Produce  application/json
// @Param data body tool.GenTemplateClassifyAddReq true "添加分类"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "data": "ok","msg": "ok"}"
// @Router /tool/codegen/classify [post]
func (g *GenTemplateClassifyCtrl) AddClassify(c *gin.Context) {
	addReq := new(tool.GenTemplateClassifyAddReq)
	if err := c.ShouldBindJSON(addReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if err := g.validStateDefault(&addReq.DefaultStatesReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailValidateError(c, err.(*moerrors.ValidateMsgError))
		return
	}
	addReq.CreatedBy = utils.GetUsernameFromGin(c)
	addReq.UpdatedBy = addReq.CreatedBy
	if ok, err := services.ServiceGroupInst.ToolGroupInst.GenTemplateClassifyServiceInst.AddClassify(addReq); err != nil {
		global.MoLogger.Error("添加分类失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommCreatFailCode, consts.TKey_TemplateClassifyAddFailMsg)
	} else if !ok {
		response.FailMsgAsI18n(c, consts.CommCreatFailCode, consts.TKey_CommCreatFailMsg)
	} else {
		response.SuccessAsI18n(c, consts.TKey_CommCreatSuccessMsg, nil)
	}
}

// DeleteClassify 删除分类
// @Tags 代码模板分类
// @Summary 删除分类
// @Security ApiKeyAuth
// @Produce  application/json
// @Param data body tool.GenTemplateClassifyDeleteReq true "删除分类"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "data": "ok","msg": "ok"}"
// @Router /tool/codegen/classify/:ids [delete]
func (g *GenTemplateClassifyCtrl) DeleteClassify(c *gin.Context) {
	idsStr := utils.GinParamGet(c, "ids")
	if idsStr == "" {
		global.MoLogger.Info("参数错误")
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	ids := utils.StrToUint64s(idsStr, ",")
	if err := services.ServiceGroupInst.ToolGroupInst.GenTemplateClassifyServiceInst.DeleteClassify(ids); err != nil {
		global.MoLogger.Error("删除分类失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommDeleteFailCode, consts.TKey_TemplateClassifyDeleteFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommDeleteSuccessMsg, nil)
}

// GetClassifyById 获取分类信息
// @Tags 代码模板分类
// @Summary 获取分类信息
// @Security ApiKeyAuth
// @Produce  application/json
// @Param data body tool.GenTemplateClassifyGetByIdReq true "获取分类信息"
// @Success 200 {object} response.Response{data=tool.GenTemplateClassifyDetailResp,msg=string} "{"code": 200, "data": "ok","msg": "ok"}"
// @Router /tool/codegen/classify/:id [get]
func (g *GenTemplateClassifyCtrl) GetClassifyById(c *gin.Context) {
	id := utils.GinParamGet(c, "id")
	if id == "" {
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	classifyDetailResp, err := services.ServiceGroupInst.ToolGroupInst.GenTemplateClassifyServiceInst.GetClassifyById(utils.StrToUint64Def(id, 0))
	if err != nil {
		global.MoLogger.Error("获取分类信息失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_TemplateClassifyGetFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, classifyDetailResp)
}

// UpdateClassify 修改分类
// @Tags 代码模板分类
// @Summary 修改分类
// @Security ApiKeyAuth
// @Produce  application/json
// @Param data body tool.GenTemplateClassifyUpdateReq true "修改分类"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "data": "ok","msg": "ok"}"
// @Router /tool/codegen/classify/:id [put]
func (g *GenTemplateClassifyCtrl) UpdateClassify(c *gin.Context) {
	updateReq := new(tool.GenTemplateClassifyUpdateReq)
	if err := c.ShouldBindJSON(updateReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if err := g.validStateDefault(&updateReq.DefaultStatesReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailValidateError(c, err.(*moerrors.ValidateMsgError))
		return
	}
	updateReq.UpdatedBy = utils.GetUsernameFromGin(c)
	if err := services.ServiceGroupInst.ToolGroupInst.GenTemplateClassifyServiceInst.UpdateClassify(updateReq); err != nil {
		global.MoLogger.Error("修改分类失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommUpdateFailCode, consts.TKey_TemplateClassifyUpdateFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommUpdateSuccessMsg, nil)
}

// DetectTemplateFiles 检测模板文件
// @Tags 代码模板分类
// @Summary 检测模板文件
// @Security ApiKeyAuth
// @Produce  application/json
// @Param data body tool.GenTemplateClassifyDetectTemplateFilesReq true "检测模板文件"
// @Success 200 {object} response.Response{data=[]string,msg=string} "{"code": 200, "data": [...],"msg": "ok"}"
// @Router /tool/codegen/classify/detect [get]
func (g *GenTemplateClassifyCtrl) DetectTemplateFiles(c *gin.Context) {
	classifyCode := c.Query("classifyCode")
	if classifyCode == "" {
		global.MoLogger.Error("参数错误", zap.Error(errors.New("classifyCode为空")))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if templateFiles, err := services.ServiceGroupInst.ToolGroupInst.GenTemplateClassifyServiceInst.DetectTemplateFiles(classifyCode); err != nil {
		global.MoLogger.Error("获取模板文件失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommSelectFailCode, consts.TKey_TemplateClassifyGetFileFailMsg)
		return
	} else {
		response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, templateFiles)
	}
}

// ChangeStatus 修改状态
// @Tags 代码模板分类
// @Summary 修改状态
// @Security ApiKeyAuth
// @Produce  application/json
// @Param data body tool.ChangeClassifyStatusReq true "修改状态"
// @Success 200 {object} response.Response{msg=string} "{"code": 200, "data": "ok","msg": "ok"}"
// @Router /tool/codegen/classify/changeStatus [put]
func (g *GenTemplateClassifyCtrl) ChangeStatus(c *gin.Context) {
	changeStatusReq := new(request.ChangeStatusReq)
	if err := c.ShouldBindJSON(changeStatusReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if err := services.ServiceGroupInst.ToolGroupInst.GenTemplateClassifyServiceInst.ChangeStatus(changeStatusReq); err != nil {
		global.MoLogger.Error("修改状态失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommUpdateFailCode, consts.TKey_CommUpdateStatusFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommUpdateStatusSuccessMsg, nil)
}

func (g *GenTemplateClassifyCtrl) validStateDefault(req *tool.DefaultStatesReq) error {
	if req.Status == consts2.U_DISABLE && req.IsDefault == consts2.U_YES {
		msg, _ := utils.Translater.Translate(consts.TKey_TemplateClassifyDefaultTplFailMsg)
		return moerrors.NewValidateError(consts.ValidatorParamsCheckFailCode, msg)
	}
	return nil
}
